<template>
  <div>
    <!-- 搜索框 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle"> </TopFilter>

    <!-- 问题信息 -->
    <FilterTable
      ref="chooseProblemGroopTable"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :height="'auto'"
      :selectionType="'multiple'"
      columnsRef="ysjProbemSelect2019092301"
      :onPageChange="clearProblemTable"
      :onRowSelectChange="arr => (this.chooseAnswer = arr)"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>

    <!-- 答案信息 -->
    <FilterTable
      ref="chooseProblemTable"
      :columns="answerTableColums"
      :fetchapi="getAnswerApi"
      :params="fetchAnswerParams"
      :datakey="'records'"
      :height="'height:auto'"
      :isSelectColumn="false"
      :isToperInfo="false"
      :isColumnFilter="false"
      columnsRef="ysjProbemSelect2019092302"
      :onSyncTableData="tableChangeFun"
      :onColumnsChange="columns => (this.answerTableColums = columns)"
    >
    </FilterTable>
    <div style="text-align:center">
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="req">确 定</el-button>
        <el-button type="primary" @click="goback">取 消</el-button>
      </span>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { getQuestionnaireInfo, queryQuestionPagById } from '@/api/customerRepair/visitRules/questionnaireMake2';

export default {
  name: 'ProblemSelect',
  components: {},
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    this.AnswerTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: getQuestionnaireInfo, // 获取问题表格接口
      fetchParams: { xhrAbort: false, dmsFuncId: 404025 }, // 获取问题表格参数
      columns: this.createTableColumns(), // 问题信息表格列
      getAnswerApi: queryQuestionPagById, // 获取选项表格接口
      fetchAnswerParams: { xhrAbort: true }, // 获取选项表格参数
      answerTableColums: this.createProblemTableColums(), // 选项信息表格列
      chooseAnswer: [], // 选择的问题
      tableData: []
    };
  },
  mounted() {
    this.BaseTable = this.$refs.chooseProblemGroopTable.$refs.pageTable;
    this.AnswerTable = this.$refs.chooseProblemTable.$refs.pageTable;
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.getProblemById(props.row)}>
                  问题选项
                </el-button>
              </div>
            );
          }
        },
        {
          title: '问题名称',
          dataIndex: 'questionName',
          filterType: 'input'
        },
        {
          title: '问题内容',
          dataIndex: 'questionContent',
          filterType: 'input'
        },
        {
          title: '是否启用',
          dataIndex: 'isValid',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.isValid, '1004')}</div>;
          }
        },
        {
          title: '是否必填',
          dataIndex: 'isMustFilled',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.isMustFilled, '1004')}</div>;
          }
        },
        {
          title: '数据来源',
          dataIndex: 'dataSources',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.dataSources, '1045')}</div>;
          }
        },
        {
          title: '使用次数',
          dataIndex: 'questionNumber',
          filterType: 'input'
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '问题名称',
          fieldName: 'questionName',
          placeholder: '问题名称'
        },
        {
          type: 'INPUT',
          label: '问题类型',
          fieldName: 'questionType',
          placeholder: '问题类型'
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'isValid',
          placeholder: '是否启用',
          itemList: this.createDictList('1004')
        }
      ];
    },
    createProblemTableColums() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '答案组ID',
          dataIndex: 'answerGroupId',
          filterType: 'input'
        },
        {
          title: '答案名称',
          dataIndex: 'answerGroupName',
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '答案说明',
          dataIndex: 'answerGroupDescript',
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '答案选项',
          dataIndex: 'answerOption',
          sorter: true,
          filterType: 'input',
          render: props => {
            let res = '<span>';
            if (props.row.questionType === 15711001 || props.row.questionType === 15711002) {
              if (props.row.answerOption != '') {
                let answerOption = props.row.answerOption.split(',');
                let type = props.row.questionType === 15711001 ? 'radio' : 'checkbox';
                for (let i = 0; i < answerOption.length; i++) {
                  res += "<label><input name='Fruit' type='" + type + "' value='' />" + answerOption[i] + ' </label> ';
                }
              }
            }
            res += '</span>';
            return <p domPropsInnerHTML={res}></p>;
          }
        }
      ];
    },
    // 搜索
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
      // 清空选项信息表格
      this.clearProblemTable();
    },
    // 保存problemTable的数据，用于清空
    tableChangeFun(val) {
      this.tableData = val;
    },
    // 清空problemTable
    clearProblemTable() {
      this.$nextTick(() => {
        this.AnswerTable.EXECUTE_DELETE(this.tableData);
      });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val) {
      this.visible = val;
    },
    // 取消
    goback() {
      this.$emit('fatherGoBack');
    },
    // 获取问题选项
    getProblemById(row) {
      let vat = {
        questionId: row.questionId
      };
      this.fetchAnswerParams = Object.assign({}, vat, { xhrAbort: false });
    },
    // 确定选择问题
    req() {
      if (this.chooseAnswer.length === 0) {
        this.$message.error('请选择一个问题');
        return;
      }
      this.$emit('fatherReq', this.chooseAnswer);
    }
  }
};
</script>

<style scoped>
.dialog-form {
  width: 100%;
  margin: 0;
}

.el-checkbox-group .el-checkbox + .el-checkbox {
  margin: 0;
}
</style>
